package pers.lws.erp.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.lws.erp.common.emum.MsgStatusEmum;
import pers.lws.erp.common.result.ResultBody;
import pers.lws.erp.entity.ProductionPlanProduct;
import pers.lws.erp.service.IProductionPlanProductService;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liuws
 * @since 2020-09-22
 */
@RestController
@RequestMapping("/productionPlanProduct")
@Slf4j
public class ProductionPlanProductController {

    @Autowired
    private IProductionPlanProductService productionPlanProductService;

    @PostMapping("/addRegisterProduct")
    public ResultBody addRegisterProduct(@RequestBody List<ProductionPlanProduct> productionPlanProduct) {

        boolean save = productionPlanProductService.saveBatch(productionPlanProduct);
        return save ? ResultBody.success(MsgStatusEmum.ADD_SUCCESS.getMsg(), null) : ResultBody.error(MsgStatusEmum.ADD_ERROR.getMsg());
    }

    @GetMapping("/findPlanProduct/{productionId}")
    public ResultBody findPlanProduct(@PathVariable String productionId) {
        log.info(productionId);
        List<ProductionPlanProduct> planProduct = productionPlanProductService.findPlanProduct(productionId);
        return planProduct.isEmpty() ? ResultBody.error("没有数据") : ResultBody.success("查询成功", planProduct);
    }

    @GetMapping("/findAllPlan/{checked}/{dispatchCheck}")
    public ResultBody findAllPlan(@PathVariable Integer checked,
                                  @PathVariable Integer dispatchCheck) {
        List<ProductionPlanProduct> allPlan = productionPlanProductService.findAllPlan(checked,dispatchCheck);
        return ResultBody.success("", allPlan);
    }

    @PutMapping("/updateDispatch/{productionId}/{productId}/{dispatchCheck}")
    public ResultBody updateDispatch(@PathVariable String productionId,
                                     @PathVariable String productId,
                                     @PathVariable Integer dispatchCheck) {
        UpdateWrapper<ProductionPlanProduct> wrapper = new UpdateWrapper<>();
        wrapper.set("dispatch_check", dispatchCheck);
        wrapper.eq("production_id", productionId);
        wrapper.eq("product_id", productId);
        boolean update = productionPlanProductService.update(wrapper);
        return update ? ResultBody.success("成功！", null) : ResultBody.error("失败！");
    }
}

